package com.announcement.dao.Impl;

import org.springframework.stereotype.Component;

import com.announcement.dao.MessageDao;
import com.announcement.entity.Message;
import com.announcement.entity.vo.Page;

@Component
public class MessageDaoImpl extends BaseDaoImpl<Message, Integer> implements
		MessageDao {

	@Override
	public Page<Message> showArticleToHome(int pageIndex) {
		String hql = "from Message M order by M.publishDate desc";
		Page<Message> page = (Page<Message>) this.findPage(10, pageIndex, hql);
		System.out.println(page);
		return page;
	}

	@Override
	public Page<Message> showMyArticle(int pageIndex, int userId) {
		String hql = "from Message M where M.user.userId ='" + userId + "'";
		Page<Message> msglist = (Page<Message>) this.findPage(10, pageIndex,
				hql);
		return msglist;
	}

	@Override
	public boolean deleteArticle(int messId) {
		this.delete(messId);
		return true;
	}

	@Override
	public boolean saveArticle(Message msg) {
		int status = (int) this.save(msg);
		if (status == 0) {
			return false;
		}
		return true;
	}

	@Override
	public Message showArticleById(int messId) {
		Message msg = this.get(messId);
		return msg;
	}

	@Override
	public boolean updateArticle(Message msg) {
		this.update(msg);
		return true;
	}

	@Override
	public Page<Message> searchArticle(int pageIndex, String keywords) {
		String hql = "from Message M where M.title like '%" + keywords
				+ "%' order by M.publishDate desc";
		return this.findPage(10, pageIndex, hql);
	}

}
